using System;
using System.Collections.Generic;
using System.Text;
using Barrage.Global;
using Barrage;
using Barrage.Items;
using Barrage.Textures;
using Spacedash.Logic;
using Spacedash.Game.Items;
using SlimDX;
using Barrage.Elements;

namespace Spacedash.Game.Nodes
{
    /// <summary>
    /// Node that holds all the items that form a ship : An item and some particle emitters.
    /// It moves a little the ship model to show a subtle oscillation without altering the game logic
    /// </summary>
    public class ShipNode : Node
    {
        Ship ship;
        /// <summary>
        /// Ship associated to this node
        /// </summary>
        public Ship Ship { get { return ship; } }

        Vector3 shipBasePosition;
        /// <summary>
        /// Base position of the associated ship
        /// </summary>
        public Vector3 ShipBasePosition { get { return shipBasePosition; } }

         ShipItem shipItem;
        /// <summary>
        /// ShipItem inside this node
        /// </summary>
        public ShipItem ShipItem { get { return shipItem; } }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="ship">Ship associated to this node</param>
        public ShipNode(Ship ship)
        {
            this.ship = ship;
            shipItem = new ShipItem(ship);
            this.addElement(shipItem);
            ParticleEmitter emitter=new ParticleEmitter(TextureManager.Textures["Spark.dds"]);
            Vector3 thrusterOffset = ship.Profile.ShipModelInstance.ThrusterOffset;
            emitter.Motion.Translation = thrusterOffset;// new Vector3(0.8f, -0.05f, -1.75f);
            addElement(emitter);
            Item item = new Item(emitter.Mesh);
            item.Motion.Translation = new Vector3(-thrusterOffset.X, thrusterOffset.Y, thrusterOffset.Z);//- 0.8f, -0.05f, -1.75f);
            addElement(item);
        }
        /// <summary>
        /// \internal Updates position and items inside the node
        /// </summary>
        protected override void DoUpdate()
        {
            Vector3 offset = ship.Normal * 3;
            shipBasePosition = ship.Motion.Translation + offset;
            float heightOscille = (float)Math.Sin(Timer.DefaultTimer.TotalTime / 1000.0f) * 0.125f;
            float lateralOscille = (float)Math.Sin(Timer.DefaultTimer.TotalTime / 1200.0f) * 0.025f;
            float roll = (float)Math.Sin(Timer.DefaultTimer.TotalTime / 500.0f) * 0.05f;
            offset += ship.Normal * heightOscille;
            offset.X = 0;
            offset += new Vector3(lateralOscille, 0, 0);
            motion.Assign(ship.Motion);
            motion.Translation += offset;

            base.DoUpdate();
        }
    }
}
